package com.easychat.mychat.userservice.Controller;

import com.easychat.mychat.common.Controller.ABaseController;

import com.easychat.mychat.common.annotation.GlobalInteceptor;
import com.easychat.mychat.common.entity.dto.TokenUserInfoDto;
import com.easychat.mychat.common.entity.dto.UserContactSearchResultDto;
import com.easychat.mychat.common.entity.enums.PageSize;
import com.easychat.mychat.common.entity.enums.ResponseCodeEnum;
import com.easychat.mychat.common.entity.enums.UserContactStatusEnum;
import com.easychat.mychat.common.entity.enums.UserContactTypeEnum;
import com.easychat.mychat.common.entity.vo.PaginationResultVO;
import com.easychat.mychat.common.entity.vo.ResponseVO;
import com.easychat.mychat.common.entity.vo.UserInfoVO;
import com.easychat.mychat.common.exception.BusinessException;
import com.easychat.mychat.userservice.Mapper.UserContactMapper;
import com.easychat.mychat.userservice.Service.UserContactApplyService;
import com.easychat.mychat.userservice.Service.UserContactService;
import com.easychat.mychat.userservice.Service.UserInfoService;
import com.easychat.mychat.userservice.entity.po.UserContact;
import com.easychat.mychat.userservice.entity.po.UserContactApply;
import com.easychat.mychat.userservice.entity.query.UserContactApplyQuery;
import com.easychat.mychat.userservice.entity.query.UserContactQuery;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/6
 */
@RestController
@RequestMapping("/contact")
public class UserContactController extends ABaseController {
    @Resource
    private UserContactService userContactService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private UserContactMapper userContactMapper;
    @Resource
    private UserContactApplyService userContactApplyService;

    @RequestMapping("/search")
    @GlobalInteceptor
    public ResponseVO search(String contactId, HttpServletRequest request){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserContactSearchResultDto userContact =  userContactService.searchContact(tokenUserInfo.getUserId(),contactId);


        return getSuccessResponseVO(userContact);
    }


//    @RequestMapping("/loadContact")
//    @GlobalInteceptor
//    public ResponseVO loadContact(String contactType, HttpServletRequest request){
//        UserContactTypeEnum byName = UserContactTypeEnum.getByName(contactType);
//        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
//        UserContactQuery query = new UserContactQuery();
//        query.setContactType(byName.getType());
//        query.setUserId(tokenUserInfo.getUserId());
//        query.setStatus(UserContactStatusEnum.FRIEND.getStatus());
//        query.setOrderBy("create_time desc");
//        List<UserContact> listByParam = userContactService.findListByParam(query);
//
//        return getSuccessResponseVO(listByParam);
//    }

    @RequestMapping("/applyAdd")
    @GlobalInteceptor
    public ResponseVO applyAdd(HttpServletRequest request,
                               @NotEmpty String contactId,
                               @NotEmpty String contactType,
                               String applyInfo){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

        userContactService.addApply(tokenUserInfo,contactId,applyInfo);


        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadApply")
    @GlobalInteceptor
    public ResponseVO loadApply(HttpServletRequest request,
                               Integer pageNo){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

        UserContactApplyQuery query = new UserContactApplyQuery();
        query.setOrderBy("last_apply_time desc");
        query.setReceiveUserId(tokenUserInfo.getUserId());
        query.setPageNo(pageNo);
        query.setPageSize(PageSize.SIZE15.getSize());
        query.setQueryContactInfo(true);

        PaginationResultVO<UserContactApply> listByPage = userContactApplyService.findListByPage(query);
        return getSuccessResponseVO(listByPage);

    }

    @RequestMapping("/dealWithApply")
    @GlobalInteceptor
    public ResponseVO dealWithApply(HttpServletRequest request,
                                @NotEmpty Integer applyId,Integer status){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        userContactApplyService.dealWithApply(tokenUserInfo.getUserId(),applyId,status);


        return getSuccessResponseVO(null);

    }

    @RequestMapping("/loadContact")
    @GlobalInteceptor
    public ResponseVO loadContact(HttpServletRequest request,
                                    @NotNull String contactType){
        UserContactTypeEnum byName = UserContactTypeEnum.getByName(contactType);
        if (byName == null){
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserContactQuery query = new UserContactQuery();
        query.setUserId(tokenUserInfo.getUserId());
        query.setOrderBy("last_update_time desc");
        query.setContactType(byName.getType());

        if (byName == UserContactTypeEnum.USER) {
            query.setQueryContactUserInfo(true);
        }else if (byName == UserContactTypeEnum.GROUP){
            query.setQueryGroupInfo(true);
            query.setExcludeMyGroup(true);
        }
        Integer[] statusArray = new Integer[]{UserContactStatusEnum.FRIEND.getStatus(),UserContactStatusEnum.DEL_BE.getStatus(),UserContactStatusEnum.BLACKLIST_BE.getStatus()};
        query.setStatusArray(statusArray);
        List<UserContact> listByParam = userContactService.findListByParam(query);

        return getSuccessResponseVO(listByParam);
    }

    @RequestMapping("/getContactUserInfo")
    @GlobalInteceptor
    public ResponseVO getContactUserInfo(HttpServletRequest request,
                                  @NotNull String contactId){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserInfoVO userInfoVO = userInfoService.getContactInfo(tokenUserInfo,contactId);

        return getSuccessResponseVO(userInfoVO);
    }

    @RequestMapping("/getContactInfo")
    @GlobalInteceptor
    public ResponseVO getContactInfo(HttpServletRequest request,
                                         @NotNull String contactId){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserContactQuery query = new UserContactQuery();
        query.setContactId(contactId);
        query.setUserId(tokenUserInfo.getUserId());

        List<UserContact> listByParam = userContactService.findListByParam(query);

        if (listByParam == null){
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        UserInfoVO userInfoVO = userInfoService.getContactInfo(tokenUserInfo,contactId);

        return getSuccessResponseVO(userInfoVO);
    }

    @RequestMapping("/delContact")
    @GlobalInteceptor
    public ResponseVO delContact(HttpServletRequest request,
                                         @NotNull String contactId){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        userInfoService.changeContact(tokenUserInfo,contactId,UserContactStatusEnum.DEL.getStatus());

        return getSuccessResponseVO(null);
    }

    @RequestMapping("/addContact2BlackList")
    @GlobalInteceptor
    public ResponseVO addContact2BlackList(HttpServletRequest request,
                                 @NotNull String contactId){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        userInfoService.changeContact(tokenUserInfo,contactId,UserContactStatusEnum.BLACKLIST.getStatus());

        return getSuccessResponseVO(null);
    }

    @RequestMapping("/getUserContactCount")
    @GlobalInteceptor
    public Integer getUserContactCount(UserContactQuery query){
        return userContactMapper.selectCount(query);
    }
}
